//
//  ZGLMultiplesKeyboard.m
//  ZeegingLottery
//
//  Created by 李曌 on 2018/7/2.
//  Copyright © 2018年 蒂埃里. All rights reserved.
//

#import "ZGLMultiplesKeyboard.h"

@interface ZGLMultiplesKeyboard ()
{
    CGFloat contentViewH;
    NSInteger beiShuNum;
    
}

@property (nonatomic, strong)UIView *contentView;

@property (nonatomic, strong)UIButton *doneBtn;   //
@property (nonatomic, strong)UILabel *jiaLabel;
@property (nonatomic, strong)UILabel *beiLabel;
@property (nonatomic, strong)UILabel *beiShuLabel;  //最终倍数

@property (nonatomic, strong)UIView *doublingView;
@property (nonatomic, strong)UIView *normalNum;



@end

@implementation ZGLMultiplesKeyboard

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupContentUI];
        
    }
    
    return self;
}

- (void)dealloc
{
    DLog(@"%s",__func__);
}

- (void)setBeiShuNumStr:(NSString *)beiShuNumStr
{
    _beiShuNumStr = beiShuNumStr;
    self.beiShuLabel.text = beiShuNumStr;
    
}

- (void)setupContentUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = sRGBHexAlpha(0xC0C0C0, 0.5);
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)]];
    
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    self.contentView = contentView;
    [self addSubview:self.contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.bottom.equalTo(self);
//        make.height.equalTo(@(320));
    }];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneBtn setBackgroundImage:[UIImage imageNamed:@"feedbackDown"] forState:UIControlStateNormal];
    doneBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 10);
    [doneBtn addTarget:self action:@selector(dissMissKeyboard) forControlEvents:UIControlEventTouchUpInside];
    self.doneBtn = doneBtn;
    [_contentView addSubview:self.doneBtn];
    [_doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_contentView).offset(10);
        make.right.equalTo(_contentView).offset(-20);
        make.size.equalTo(@(30));
        
    }];
    
    UILabel *jiaLabel = [[UILabel alloc]init];
    jiaLabel.text = @"投";
    jiaLabel.textColor = [UIColor darkGrayColor];
    jiaLabel.font = [UIFont systemFontOfSize:14];
    self.jiaLabel = jiaLabel;
    [_contentView addSubview:self.jiaLabel];
    [_jiaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_contentView).offset(20);
        make.centerY.equalTo(_doneBtn);
        
    }];
    
    UILabel *beiShuLabel = [[UILabel alloc]init];
    beiShuLabel.layer.borderWidth = kBorderWidth;
    beiShuLabel.layer.cornerRadius = 4;
    beiShuLabel.layer.masksToBounds = YES;
    beiShuLabel.layer.borderColor = BorderColor.CGColor;
    beiShuLabel.backgroundColor = ViewBackgroundColor;
//    beiShuLabel
    beiShuLabel.textAlignment = 1;
    beiShuLabel.text = @"";
    beiShuLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    self.beiShuLabel = beiShuLabel;
    [_contentView addSubview:self.beiShuLabel];
    [_beiShuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_jiaLabel.mas_right).offset(5);
        make.centerY.equalTo(_jiaLabel);
        make.width.equalTo(@(55));
        make.height.equalTo(@(25));
        
    }];
    
    UILabel *beiLabel = [[UILabel alloc]init];
    beiLabel.textColor = [UIColor darkGrayColor];
    beiLabel.text = @"倍";
    beiLabel.font = [UIFont systemFontOfSize:14];
    self.beiLabel = beiLabel;
    [_contentView addSubview:self.beiLabel];
    [_beiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(_beiShuLabel.mas_right).offset(5);
        make.centerY.equalTo(_jiaLabel);
    }];
    
    UIView *doublingView = [[UIView alloc]init];
    self.doublingView = doublingView;
    [_contentView addSubview:self.doublingView];
    [_doublingView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_doneBtn.mas_bottom).offset(10);
        make.left.equalTo(_contentView);
        make.right.equalTo(_contentView);
        
    }];
    [self creatDoublingBtnWith:5 Warp:5];
    
    UIView *normalNum = [[UIView alloc]init];
    self.normalNum = normalNum;
    [_contentView addSubview:self.normalNum];
    [_normalNum mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_doublingView.mas_bottom);
        make.left.equalTo(_contentView);
        make.right.equalTo(_contentView);
    }];
    [self creatNormalKeyboardWith:9 Warp:3];
    
    //
    CGFloat bottomViewW = UI_SCREEN_WIDTH/3;
    CGFloat bottomViewH = IS_IPHONE_X ? 80:50;
//    CGFloat bottomViewH = 50;
    
    UIView *confirmBtnBgView = [[UIView alloc]init];
//    confirmBtnBgView.backgroundColor = [UIColor redColor];
//    confirmBtnBgView.layer.borderWidth = BorderWidth;
//    confirmBtnBgView.layer.borderColor = BorderColor.CGColor;
    [_contentView addSubview:confirmBtnBgView];
    [confirmBtnBgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(_contentView);
        make.top.equalTo(_normalNum.mas_bottom);
        make.bottom.equalTo(_contentView);
        make.width.equalTo(@(bottomViewW));
        make.height.equalTo(@(bottomViewH));
    }];
    
    UIView *deleteBgView = [[UIView alloc]init];
    deleteBgView.backgroundColor = [UIColor whiteColor];
//    deleteBgView.layer.borderWidth = BorderWidth;
//    deleteBgView.layer.borderColor = BorderColor.CGColor;
    [_contentView addSubview:deleteBgView];
    [deleteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_normalNum.mas_bottom);
        make.right.equalTo(_contentView);
        make.bottom.equalTo(_contentView);
        make.width.equalTo(@(bottomViewW));
        make.height.equalTo(@(bottomViewH));
    }];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setBackgroundColor:globalRed];
    [confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    confirmBtn.layer.borderWidth = kBorderWidth;
    confirmBtn.layer.borderColor = BorderColor.CGColor;
    [deleteBgView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(deleteBgView);
        make.left.equalTo(deleteBgView);
        make.right.equalTo(deleteBgView);
        make.height.equalTo(@(50));
    }];
    
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [cancelBtn setTitle:@"X" forState:UIControlStateNormal];
    //    [cancelBtn setTitleColor:mainTextColor forState:UIControlStateNormal];
    //    cancelBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [deleteBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [deleteBtn setImage:[UIImage imageNamed:@"cross"] forState:UIControlStateNormal];
    deleteBtn.layer.borderWidth = kBorderWidth;
    deleteBtn.layer.borderColor = BorderColor.CGColor;
    [confirmBtnBgView addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(confirmBtnBgView);
        make.left.equalTo(confirmBtnBgView);
        make.right.equalTo(confirmBtnBgView);
        make.height.equalTo(@(50));
    }];
    
    UIView *zeroBgView = [[UIView alloc]init];
    zeroBgView.backgroundColor = [UIColor whiteColor];
//    zeroBgView.layer.borderWidth = BorderWidth;
//    zeroBgView.layer.borderColor = BorderColor.CGColor;
    [_contentView addSubview:zeroBgView];
    [zeroBgView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(confirmBtnBgView.mas_right);
        make.top.equalTo(_normalNum.mas_bottom);
        make.bottom.equalTo(_contentView);
        make.width.equalTo(@(bottomViewW));
        make.height.equalTo(@(bottomViewH));

    }];
    UIButton *zeroBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [zeroBtn addTarget:self action:@selector(zeroBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [zeroBtn setTitle:@"0" forState:UIControlStateNormal];
    [zeroBtn setTitleColor:mainTextColor forState:UIControlStateNormal];
    zeroBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    zeroBtn.layer.borderWidth = kBorderWidth;
    zeroBtn.layer.borderColor = BorderColor.CGColor;
    [zeroBgView addSubview:zeroBtn];
    [zeroBtn mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(zeroBgView);
        make.left.equalTo(zeroBgView);
        make.right.equalTo(zeroBgView);
        make.height.equalTo(@(50));
    }];

    
    
}

//整数倍
- (void)creatDoublingBtnWith:(NSInteger )count Warp:(NSInteger )warp
{
    NSArray *arr = @[@"1",@"5",@"10",@"20",@"50"];
    for (int i = 0; i<count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:btnFontSize];
        button.layer.borderWidth = kBorderWidth;
        button.layer.borderColor = BorderColor.CGColor;
        button.tag = [arr[i] integerValue];
        [button addTarget:self action:@selector(doubleChange:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [button setTitle:[NSString stringWithFormat:@"%@倍",arr[i]] forState:UIControlStateNormal];
        
        [self.doublingView addSubview:button];
    }
    [self.doublingView.subviews mas_distributeSudokuViewsWithFixedItemWidth:0 fixedItemHeight:40 fixedLineSpacing:0 fixedInteritemSpacing:0 warpCount:warp topSpacing:0 bottomSpacing:0 leadSpacing:0 tailSpacing:0];
    
}

//累加倍
- (void)creatNormalKeyboardWith:(NSInteger )count Warp:(NSInteger )warp
{
    NSArray *arr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
    for (int i = 0; i<count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
        button.layer.borderWidth = kBorderWidth;
        button.layer.borderColor = BorderColor.CGColor;
        button.tag = i+1;
        [button addTarget:self action:@selector(normalChange:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:mainTextColor forState:UIControlStateNormal];
        [button setTitle:[NSString stringWithFormat:@"%@",arr[i]] forState:UIControlStateNormal];
        
        [self.normalNum addSubview:button];
    }
    
    [self.normalNum.subviews mas_distributeSudokuViewsWithFixedItemWidth:0 fixedItemHeight:50 fixedLineSpacing:0 fixedInteritemSpacing:0 warpCount:warp topSpacing:0 bottomSpacing:0 leadSpacing:0 tailSpacing:0];
    
}




#pragma mark - method

//0
- (void)zeroBtnClick:(UIButton *)sender
{
    DLog(@"0");
    NSString *str = self.beiShuLabel.text;
    if ((str.length>0 && str.length < 3) || [str integerValue]==100) {
        
        self.beiShuLabel.text = [str stringByAppendingString:[@(sender.tag) stringValue]];
    }
    
}


//确定
- (void)confirmBtnClick:(UIButton *)sender
{
    DLog(@"确定加倍");
    NSString *str = self.beiShuLabel.text;
    if (str.length>0 && self.numBlock) {
        self.numBlock([str integerValue]);
    }
    [self dismiss];
}

// 删除
- (void)cancelBtnClick:(UIButton *)sender
{
    DLog(@"删除加倍");
    NSString *str = self.beiShuLabel.text;
    if (str.length>0) {
        
        self.beiShuLabel.text = [PublicMethod removeLastOneChar:str];
    }
    
}


//累加
- (void)normalChange:(UIButton *)sender
{
    DLog(@"%ld",sender.tag);
    NSString *str = self.beiShuLabel.text;
    
    if (str.length < 3) {

        self.beiShuLabel.text = [str stringByAppendingString:[@(sender.tag) stringValue]];
    }
    
}

//直接翻倍
- (void)doubleChange:(UIButton *)sender
{
    DLog(@"%ld",sender.tag);
    beiShuNum = sender.tag;
    self.beiShuLabel.text = [NSString stringWithFormat:@"%@",[@(beiShuNum) stringValue]];
    
}

- (void)dissMissKeyboard
{
    DLog(@"收键盘");
    [self dismiss];
    
}


//弹出
-(void)showView
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    
    self.contentView.y = UI_SCREEN_HEIGHT;
    contentViewH = IS_IPHONE_X ? 380:350;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.contentView.y = UI_SCREEN_HEIGHT - contentViewH;
    }];
    
    
}

//回收键盘
- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
        
        self.contentView.y = UI_SCREEN_HEIGHT;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        [_contentView removeFromSuperview];
        
    }];
    
}

@end
